package com.juzi.wopu.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.juzi.wopu.domain.Order;
import com.juzi.wopu.domain.Type;
import com.juzi.wopu.query.OrderQuery;
import com.juzi.wopu.service.IOrderService;

public class OrderServiceImpl extends BaseServiceImpl<Order> implements IOrderService{

	@Override
	public Map<String, String> getTotalCount(OrderQuery query) {
		Map<String, String> map=new HashMap<>();
		
		String sql="select sum(iCount),sum(dPrice) FROM tCartCheck where 1=1 ";
		
		if(query.getBeginDate()!=null && query.getEndDate()!=null){
			SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			sql=sql+" and dtInsert > '"+dateFormat.format(query.getBeginDate())+
					"' and dtInsert < '"+dateFormat.format(query.getEndDate())+"'";
		}
		
		int totalNum=-1;
		double totalMoney=-1;
		ResultSet resultSet=BaseDao.executeQuerySql(sql);
		if(resultSet!=null){
			try {
				while(resultSet.next()){
					totalNum=resultSet.getInt(1);
					totalMoney=resultSet.getDouble(2);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		map.put("totalNum", totalNum+"");
		map.put("totalMoney", totalMoney+"");
		return map;
	}

	@Override
	public List<Type> getAllType() {
		String sql="select iType,iTypeSub from tOrder group by iType,iTypeSub";
		ResultSet resultSet=BaseDao.executeQuerySql(sql);
		List<Type> list=new ArrayList<>();
		try {
			while(resultSet.next()){
				Type type=new Type();
				type.setTypeCode(resultSet.getLong(1));
				type.setSubTypeCode(resultSet.getLong(2));
				list.add(type);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
}
